﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.ActiveXPartLib
{
    /// <summary>
    /// 视频预加载器
    /// </summary>
    public class INSVideoPreloader : IPreloader
    {
        public SingletonINSVideoPro _pro { get; private set; }

        private string _scene;
        private string _part;

        public void Start(CPPEI.Coolzon.Metadata.Scene scene, CPPEI.Coolzon.Metadata.Part part)
        {
            // 获取关键属性
            //"Url""UserName""Password","CameraID","Mode","LoginClientStrategy""LocalVideoFileName"
            string xUrl = null;
            string xPassword = null;
            string xUserName = null;
            int xCameraID = 0;
            string xMode = null;
            string xLoginClientStrategy = null;
            string xLocalVideoFileName = null;
            for (int index = 0; index < part.Properties.Count; index++)
			{
                if(part.Properties[index].Name == "Url")
                {
                    xUrl = part.Properties[index].ValueString;
                }
                else  if(part.Properties[index].Name == "UserName")
                {
                    xUserName = part.Properties[index].ValueString;
                }
                else if (part.Properties[index].Name == "Password")
                {
                    xPassword = part.Properties[index].ValueString;
                }
                else  if(part.Properties[index].Name == "CameraID")
                {
                    xCameraID = int.Parse(part.Properties[index].ValueString);
                }
                else  if(part.Properties[index].Name == "Mode")
                {
                    xMode = part.Properties[index].ValueString;
                }
                else  if(part.Properties[index].Name == "LoginClientStrategy")
                {
                    xLoginClientStrategy = part.Properties[index].ValueString;
                }
                else if (part.Properties[index].Name == "LocalVideoFileName")
                {
                    xLocalVideoFileName = part.Properties[index].ValueString;
                }
			}

            _scene = scene.Name;
            _part = part.Name;

            _pro = SingletonINSVideoPro.Start();
            this._pro.OpenCamera(xUrl, xUserName, xPassword, xCameraID, xLoginClientStrategy, xLocalVideoFileName, part.Name, scene.Name);
        }

        public void Stop()
        {
            // 关闭当前组件对应的窗口
            _pro.CloseCameraWindow(_scene, _part);
        }
    }
}
